//
//  Router.swift
//  XNavigator_Example
//
//  Created by x on 2024/9/1.
//  Copyright © 2024 CocoaPods. All rights reserved.
//

import XNavigator

/// CaseIterable
enum DefaultRoute {
    static var allCases: [DefaultRoute] = []
    
    case ARoute
    case Boute
    case CRoute
    case DRoute
    case alert(title: String, message: String)
    case webRoute(url: URL)
}

extension DefaultRoute: AppRoute {
    var viewController: UIViewController {
        switch self {
        case .ARoute:
            return A_VController()
        case .Boute:
            return B_VController()
        case .CRoute:
            return C_VController()
        case .DRoute:
            return D_VController()
        case .alert(let title, let message):
            let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
            alertController.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
            return alertController
            
        case .webRoute(let url):
            let webVc = WebViewController()
            webVc.load(url: url)
            return webVc
        }
        
    }
    
    var parameters: [String : Any]? {
        switch self {
        case .DRoute:
            return ["name": "CRoute:parameters"]
        default:
            return nil
        }
    }
    
    static func from(url: URLConvertible) -> DefaultRoute? {
        let path = url.urlStringValue
        if path == "/A" {
            return .ARoute
        } else if path.starts(with: "/B/") {
            return .Boute
        } else if path.starts(with: "/webview/") {
            guard let url = URL(string: path.replacingOccurrences(of: "/webview/", with: "")) else {
                return nil
            }
            return .webRoute(url: url)
        } else {
            return nil
        }
    }
    
    var transitionStyle: TransitionStyle {
        switch self {
        case .ARoute:
                .push(from: nil, animated: true)
        case .Boute:
                .present(from: nil, wrap: UINavigationController.self, animated: true)
        case .CRoute:
                .detail(from: nil, wrap: nil)
        case .DRoute:
                .detail(from: nil, wrap: nil)
        case .alert:
                .custom { (nav, vc) in
                    vc?.present(self.viewController, animated: true, completion: nil)
                }
        case .webRoute(let _):
                .detail(from: nil, wrap: nil)
        }
        
    }
}

let route = Router<DefaultRoute>(navigationController: nil, interceptors: [AInterceptor(), BInterceptor(), CInterceptor()])
